<?php
/************************************************
 *  描述: App类
 *  作者: firmit 
 *  创建: 2013-01-15
 *  更新: 2013-01-28
 *        判断app是否存在
 *
 ************************************************/
class MC_App{

    /**
     * @var object 对象单例
     */
    static $_instance = NULL;
	
    /**
     * @var string app名称
     */
    var $_appname = NULL;

	/**
     * @var array config数组
     */
    var $_config = NULL;
    var $_old_config = NULL;
	
	var $controller_path='';
	var $model_path='';
	var $view_path='';

	var $controller = NULL;
	var $model = NULL;
	var $view = NULL;
	
    /**
     * 构造函数
     *
     * @param string $configFile 配置文件路径
     */
    function __construct($app='default', $arrConfig=array()){
		
		$this->_appname=$app;
		
		//2013-01-28
		//判断app是否存在
		if(!is_dir(APPS_DIR."/$app")){
			throw new MC_Exception("Apps $app is not exist or not readable");
			return false;
		}
		if(empty($arrConfig)){
			$confFile =  APPS_DIR."/$app/config.php";
			if(is_file($confFile) && is_readable($confFile)){
				$config = MC_Config::factory($confFile);
				$config->setNew($confFile);
				$arrConfig = $config->getData();

				if (is_array($arrConfig) && !empty($arrConfig)){
					$this->_old_config = __get_config();
					__save_config($arrConfig);
				}
			}
			$this->_config=__get_config();
		}else{
			$this->_config=$arrConfig;
		}
		
		//应用路径
		$this->controller_path = APPS_DIR."/$app/controller";
		$this->model_path = APPS_DIR."/$app/model";
		$this->view_path = APPS_DIR."/$app/view";	
		
	}
	
	/**
     * 析构函数
     *
     * @param string $configFile 配置文件路径
     */
    function __destruct(){
		__save_config($this->_old_config);
		unset($this->_config);
		unset($this->_old_config);
		unset($this->controller);
		unset($this->model);
		unset($this->view);

		
		//应用路径
		$this->controller_path = '';
		$this->model_path = '';
		$this->view_path = '';
	}

    /**
     * 保证对象不被clone
     */
    private function __clone() {}


    /**
     * 获取对象唯一实例
     *
     * @param string $configFile 配置文件路径
     * @return object 返回本对象实例
     */
    public static function getInstance($app='default'){
        if (!(self::$_instance instanceof self)){
            self::$_instance = new self();
        }
		self::$_instance->_appname=$app;
        self::$_instance->_config=__get_config();
		return self::$_instance;
    }

    /**
     * 魔术方法，用来调用对应的controller或model
     */
	function __call($class_name, $args){
		if(strpos($class_name, 'Controller') !== false){
			return $this->Controller(str_replace('Controller', '', $class_name), array('action'=>$args[0]));
		}else if(strpos($class_name, "Model") !== false){
			return $this->Model(str_replace('Model', '', $class_name), $args);
		}else{
			return false;
		}
//
//		if (!is_file($classFile) || !is_readable($classFile)){
//			throw new MC_Exception("$func_type file $classFile not exist or not readable");
//			return false;
//		}
//		require($classFile);
//		if (!class_exists($class_name, false)){
//			throw new MC_Exception("$func_type class $class_name not exist");
//			return false;
//		}
//		
//		//返回controller或model
//		if($this->controller==null || !($this->controller instanceof $class_name)){
//			$this->controller = new $controller($this->_config, $controller, $action);
//		}
//
//		//返回controller
//		return $this->controller;

	}
	
    /**
     * 返回这个App下的Contorller
     *
     * @param string $contoller controller名称
     * @param array  $params    配置数组，可选
     * @return object 返回本对象实例
     */
	 function Controller($controller, $params=array()){

		//包含控制器文件
		$controller = $controller ."Controller";
		$controllerFile = str_replace("/",DIRECTORY_SEPARATOR,($this->controller_path. "/$controller.class.php"));
		if (!is_file($controllerFile) || !is_readable($controllerFile)){
			throw new MC_Exception("controller file $controllerFile not exist or not readable");
			return null;
		}
		if(!in_array($controllerFile, get_included_files())){
			require($controllerFile);
		}
		if (!class_exists($controller, false)){
			throw new MC_Exception("controller class $controller not exist");
			return null;
		}
		if($this->controller==null || !($this->controller instanceof $controller)){
			$this->controller = new $controller($this->_config, $controller, $params['action']);
		}
		//返回controller
		return $this->controller;
	 }	
	
	/**
     * 返回这个App下的model
     *
     * @param string $contoller controller名称
     * @param array  $params    配置数组，可选
     * @return object 返回本对象实例
     */
	 function Model($model, $params=array()){
		//包含控制器文件
		$model = $model ."Model";
		$modelFile = str_replace("/",DIRECTORY_SEPARATOR,($this->model_path. "/$model.class.php"));
		if (!is_file($modelFile) || !is_readable($modelFile)){
			throw new MC_Exception("model file $modelFile not exist or not readable");
			return null;
		}
		if(!in_array($modelFile, get_included_files())){
			require($modelFile);
		}
		if (!class_exists($model, false)){
			throw new MC_Exception("model class $model not exist");
			return null;
		}
		if($this->model==null || !($this->model instanceof $model)){
			$this->model = new $model();
		}

		//返回Model
		return $this->model;
	 }


}

?>